##### The Politicization of Identity ####
### Replication Code ###
## Julie Faller and Noah Nathan## 


####Contents:###
#SECTION ONE: LIBRARIES/LOADING DATA
#SECTION TWO:SUMMARY STATISTICS
#SECTION THREE: RULING PARTY ANALYSIS
#SECTION FOUR: OPPOSITION PARTY ANALYSIS
	#NB:Section Four analysis will overwrite Section Three #
#SECTION FIVE: FIGURES  


#################################### SECTION ONE #####################################
######################### LIBRARIES AND LOADING DATA
###################################################################################### 

###libraries
library(foreign)
library(Zelig)
library(MatchIt)
library(cem)
library(xtable)
library(car)
library(sandwich)
library(lmtest)
library(calibrate)

############################################################
####loading data
##########################################################
###loading data
data<-read.csv(file="FallerNathan_data_from_Eifert2010.csv")
head(data)
names(data)
unique(data$country_round)

data$DumBo1[data$country_round=="Botswana1"]<-1
data$DumBo1[data$country_round!="Botswana1"]<-0
data$DumBo2[data$country_round=="Botswana2"]<-1
data$DumBo2[data$country_round!="Botswana2"]<-0
data$DumMa1[data$country_round=="Malawi1"]<-1
data$DumMa1[data$country_round!="Malawi1"]<-0
data$DumMa2[data$country_round=="Malawi2"]<-1
data$DumMa2[data$country_round!="Malawi2"]<-0
data$DumMali1[data$country_round=="Mali1"]<-1
data$DumMali1[data$country_round!="Mali1"]<-0
data$DumMali2[data$country_round=="Mali2"]<-1
data$DumMali2[data$country_round!="Mali2"]<-0
data$DumNa1[data$country_round=="Namibia1"]<-1
data$DumNa1[data$country_round!="Namibia1"]<-0
data$DumNa15[data$country_round=="Namibia1.5"]<-1
data$DumNa15[data$country_round!="Namibia1.5"]<-0
data$DumNa2[data$country_round=="Namibia2"]<-1
data$DumNa2[data$country_round!="Namibia2"]<-0
data$DumNi1[data$country_round=="Nigeria1"]<-1
data$DumNi1[data$country_round!="Nigeria1"]<-0
data$DumNi15[data$country_round=="Nigeria1.5"]<-1
data$DumNi15[data$country_round!="Nigeria1.5"]<-0
data$DumNi2[data$country_round=="Nigeria2"]<-1
data$DumNi2[data$country_round!="Nigeria2"]<-0
data$DumSo1[data$country_round=="SouthAfrica1"]<-1
data$DumSo1[data$country_round!="SouthAfrica1"]<-0
data$DumSo2[data$country_round=="SouthAfrica2"]<-1
data$DumSo2[data$country_round!="SouthAfrica2"]<-0
data$DumTa1[data$country_round=="Tanzania1"]<-1
data$DumTa1[data$country_round!="Tanzania1"]<-0
data$DumTa2[data$country_round=="Tanzania2"]<-1
data$DumTa2[data$country_round!="Tanzania2"]<-0
data$DumUg1[data$country_round=="Uganda1"]<-1
data$DumUg1[data$country_round!="Uganda1"]<-0
data$DumUg2[data$country_round=="Uganda2"]<-1
data$DumUg2[data$country_round!="Uganda2"]<-0
data$DumZa1[data$country_round=="Zambia1"]<-1
data$DumZa1[data$country_round!="Zambia1"]<-0
data$DumZa2[data$country_round=="Zambia2"]<-1
data$DumZa2[data$country_round!="Zambia2"]<-0
data$DumZi1[data$country_round=="Zimbabwe1"]<-1
data$DumZi1[data$country_round!="Zimbabwe1"]<-0
data$DumZi2[data$country_round=="Zimbabwe2"]<-1
data$DumZi2[data$country_round!="Zimbabwe2"]<-0

#additional dummies for new countries
data$DumGa2[data$country_round=="Ghana2"] <- 1
data$DumGa2[data$country_round!="Ghana2"] <- 0
data$DumKe2[data$country_round=="Kenya2"] <- 1
data$DumKe2[data$country_round!="Kenya2"] <- 0
data$DumMo2[data$country_round=="Mozambique2"] <- 1
data$DumMo2[data$country_round!="Mozambique2"] <- 0
data$DumSen2[data$country_round=="Senegal2"] <- 1
data$DumSen2[data$country_round!="Senegal2"] <- 0


#collapse all education information into one variable
#educ_basic ==1  = primary or less
#educ_basic ==0 = more than primary
data$educ_basic[data$educ_none==1|data$educ_primary==1] <- 1
data$educ_basic[data$educ_none!=1&data$educ_primary!=1] <- 0


##################################### SECTION TWO #############################################
#################### Summary Statistics 
###############################################################################################

############ Creating Country Level Datasets
Bots1<-data[data$country_round=="Botswana1",]
Bots2<- data[data$country_round=="Botswana2",]
Mala1<- data[data$country_round=="Malawi1",]
Mala2<-data[data$country_round=="Malawi2",]
Nami1<- data[data$country_round=="Namibia1",]
Nami15<-data[data$country_round=="Namibia1.5",]
Nami2<-data[data$country_round=="Namibia2",]
Nige1<-data[data$country_round=="Nigeria1",]
Nige15<-data[data$country_round=="Nigeria1.5",]
Nige2<-data[data$country_round=="Nigeria2",]
Sout1<-data[data$country_round=="SouthAfrica1",]
Sout2<-data[data$country_round=="SouthAfrica2",]
Tanz1<-data[data$country_round=="Tanzania1",]
Tanz2<-data[data$country_round=="Tanzania2",]
Ugan1<-data[data$country_round=="Uganda1",]
Ugan2<-data[data$country_round=="Uganda2",]
Zamb1<-data[data$country_round=="Zambia1",]
Zamb2<-data[data$country_round=="Zambia2",]
Zimb1<-data[data$country_round=="Zimbabwe1",]
Zimb2<-data[data$country_round=="Zimbabwe2",]

Ghan2<-data[data$country_round=="Ghana2",] 
Keny2<-data[data$country_round=="Kenya2",]
Moza2<-data[data$country_round=="Mozambique2",]
Sene2<-data[data$country_round=="Senegal2",] 

## Summary Function 
summary.func<-function(dataset){
		sumvec<-c(mean(na.omit(dataset$identity_ethnic_1)), mean(na.omit(dataset$ruling_party)), mean(na.omit(dataset$opposition_party)), mean(na.omit(dataset$lang_d_10)), mean(na.omit(dataset$urban)), mean(na.omit(dataset$age)), nrow(dataset))
	return(sumvec)
	}

Bots1sum<-summary.func(Bots1)
Bots2sum<-summary.func(Bots2)
Mala1sum<-summary.func(Mala1)
Mala2sum<-summary.func(Mala2)
Nami1sum<-summary.func(Nami1)
Nami15sum<-summary.func(Nami15)
Nami2sum<-summary.func(Nami2)
Nige1sum<-summary.func(Nige1)
Nige15sum<-summary.func(Nige15)
Nige2sum<-summary.func(Nige2)
Sout1sum<-summary.func(Sout1)
Sout2sum<-summary.func(Sout2)
Tanz1sum<-summary.func(Tanz1)
Tanz2sum<-summary.func(Tanz2)
Ugan1sum<-summary.func(Ugan1)
Ugan2sum<-summary.func(Ugan2)
Zamb1sum<-summary.func(Zamb1)
Zamb2sum<-summary.func(Zamb2)
Zimb1sum<-summary.func(Zimb1)
Zimb2sum<-summary.func(Zimb2)

Ghan2sum<-summary.func(Ghan2)
Keny2sum<-summary.func(Keny2)
Moza2sum<-summary.func(Moza2)
Sene2sum<-summary.func(Sene2)


summarytable<-rbind(Bots1sum, Bots2sum, Mala1sum, Mala2sum, Nami1sum, Nami15sum, Nami2sum, Nige1sum, Nige15sum, Nige2sum, Sout1sum, Sout2sum, Tanz1sum, Tanz2sum, Ugan1sum, Ugan2sum, Zamb1sum, Zamb2sum, Zimb1sum, Zimb2sum, Ghan2sum, Keny2sum, Moza2sum, Sene2sum)
dim(summarytable)

colnames(summarytable)<-c("ethnic", "ruling", "opposition", "lang", "urban", "male", "age", "elec prox", "elec comp", "N")

rownames(summarytable)<-c("Botswana 1", "Botswana 2", "Malawi 1", "Malawi 2", "Mali 1", "Mali 2", "Namibia 1", "Namibia 1.5", "Namibia 2", "Nigeria 1", "Nigeria 1.5", "Nigeria 2", "South Africa 1", "South Africa 2", "Tanzania 1", "Tanzania 2", "Uganda 1", "Uganda 2", "Zambia 1", "Zambia 2", "Zimbabwe 1", "Zimbabwe 2", "Ghana 2", "Kenya 2", "Mozambique 2", "Senegal 2")

year<-c(1999,2003,1999,2003, 2001,2002,1999,2002,2003,2000,2001,2003,2000,2002,2001,2003,2000,2002,1999,2003,1999,2004,2002,2003,2002,2002)
summarytable
summarytable<-cbind(summarytable, year)


###################################### SECTION THREE #######################################
##################### RULING PARTY ANALYSIS ###################
### Does support for the ruling party alter likelihood of ethnic id?
#############################################################################################

####Subsetting data 

ruldata<-data[, c("identity_ethnic_1", "ruling_party", "lang_d_10", "radio_daily", "region", "urban", "age", "male", "educ_basic", "elec_prox", "elec_comp", "prox_comp","DumBo1","DumBo2","DumMa1", "DumMa2", "DumMali1", "DumMali2", "DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2")]

###omitting data-
nrow(ruldata)
ruldataom<-na.omit(ruldata)
nrow(ruldataom)

#########################################################
################# Matching Within Countries ############
#########################################################

### Botswana ####

bots <- subset(ruldataom, DumBo1==1 | DumBo2==1)
bots$reg2[bots$region==2] <- 1
bots$reg2[bots$region!=2] <- 0
bots$reg3[bots$region==3] <- 1
bots$reg3[bots$region!=3] <- 0
bots$reg4[bots$region==4] <- 1
bots$reg4[bots$region!=4] <- 0

pre.bots<-imbalance(group=bots$ruling_party,data=bots, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo2", "DumMa1", "DumMa2", "DumMali1", "DumMali2", "DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.bots

agecut <- c(25, 55)
mycp <- list(age=agecut)

match.bots <- matchit(ruling_party ~ lang_d_10 + reg2 + reg3 + reg4 + DumBo1 + urban + age + male + educ_basic + radio_daily, data = bots, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo2", "DumMa1", "DumMa2", "DumMali1", "DumMali2", "DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.bots)

bots.data <- match.data(match.bots)

post.bots.bal <- imbalance(group= bots.data$ruling_party, data=bots.data, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo2", "DumMa1", "DumMa2", "DumMali1", "DumMali2", "DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.bots.bal

bots.model <- zelig(identity_ethnic_1 ~ ruling_party + lang_d_10 + reg2 + reg3 + reg4 + urban + age + male + educ_basic + radio_daily + DumBo1 , data = bots.data, model = "logit")
summary(bots.model)

set.seed(02138)
oppo.bots <-setx(bots.model, ruling_party=0)
ruling.bots <- setx(bots.model, ruling_party=1)
sim.out.bots.ru <- sim(bots.model, x = oppo.bots, x1 = ruling.bots)
summary(sim.out.bots.ru)
# Change:
#0.0340866 (-0.02300356 0.089554)


#plot(density(sim.out.bots.ru$qi$fd), main="First Difference -- Ruling Party -- Botswana", xlab="Change in Likelihood of Ethnic Id", col="coral2", lwd=2.1)
#abline(v=0, col="azure4", lty="dashed", lwd=.8)


############ Malawi ################
mala <- subset(ruldataom, DumMa1==1 | DumMa2==1)
unique(mala$region)
mala$reg2[mala$region==2] <- 1
mala$reg2[mala$region!=2] <- 0
mala$reg3[mala$region==3] <- 1
mala$reg3[mala$region!=3] <- 0

pre.mala<-imbalance(group=mala$ruling_party,data=mala, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa2", "DumMali1", "DumMali2", "DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.mala

match.mala <- matchit(ruling_party ~ lang_d_10 + reg2 + reg3 + DumMa1 + urban + age + male+ educ_basic + radio_daily, data = mala, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa2", "DumMali1", "DumMali2", "DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.mala)

mala.data <- match.data(match.mala)

post.mala.bal <- imbalance(group= mala.data$ruling_party, data=mala.data, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa2", "DumMali1", "DumMali2", "DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"), weights=mala.data$weights)
post.mala.bal

mala.model <- zelig(identity_ethnic_1 ~ lang_d_10 + ruling_party + reg2 + reg3 + urban + age + male + educ_basic +radio_daily + DumMa1, data = mala.data, model = "logit")
summary(mala.model)

set.seed(02138)
oppo.mala<-setx(mala.model, ruling_party=0)
ruling.mala <- setx(mala.model, ruling_party=1)
sim.out.mala.ru <- sim(mala.model, x = oppo.mala, x1 = ruling.mala)
summary(sim.out.mala.ru)
#-0.1156826 (-0.1593633 -0.07505253)

#plot(density(sim.out.mala.ru$qi$fd), main="Malawi", xlab="Change in Likelihood of Ethnic Id", #col="cadetblue",xlim=c(-.3,.2), lwd=2.1)
#abline(v=0, col="azure4", lty="dashed", lwd=.8)


######### NAMIBIA ###########

nami <- subset(ruldataom, DumNa1==1 |DumNa15| DumNa2==1)
unique(nami$region)
nrow(nami)

nami$reg2[nami$region==2] <- 1
nami$reg2[nami$region!=2] <- 0
nami$reg3[nami$region==3] <- 1
nami$reg3[nami$region!=3] <- 0
nami$reg4[nami$region==4] <- 1
nami$reg4[nami$region!=4] <- 0
nami$reg5[nami$region==5] <- 1
nami$reg5[nami$region!=5] <- 0
nami$reg6[nami$region==6] <- 1
nami$reg6[nami$region!=6] <- 0
nami$reg7[nami$region==7] <- 1
nami$reg7[nami$region!=7] <- 0
nami$reg8[nami$region==8] <- 1
nami$reg8[nami$region!=8] <- 0
nami$reg9[nami$region==9] <- 1
nami$reg9[nami$region!=9] <- 0
nami$reg10[nami$region==10] <- 1
nami$reg10[nami$region!=10] <- 0

pre.nami<-imbalance(group=nami$ruling_party,data=nami, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.nami

match.nami<- matchit(ruling_party ~ lang_d_10 + reg2 + reg3 + reg4 + reg5 + reg6 + reg7 +reg8+reg9+reg10+ DumNa15 + DumNa1 +urban + age + male + educ_basic+radio_daily, data = nami, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.nami)

nami.data <- match.data(match.nami)

post.nami.bal <- imbalance(group= nami.data$ruling_party, data=nami.data, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2", "DumNa15","DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.nami.bal

nami.model <- zelig(identity_ethnic_1 ~ ruling_party + lang_d_10 + reg2 + reg3 + reg4+reg5+ reg6+ reg7+reg8+reg9+reg10+urban + age + male + educ_basic + DumNa15 + DumNa1 +radio_daily , data = nami.data, model = "logit")
summary(nami.model)


set.seed(02138)
oppo.nami<-setx(nami.model, ruling_party=0)
ruling.nami <- setx(nami.model, ruling_party=1)
sim.out.nami.ru <- sim(nami.model, x = oppo.nami, x1 = ruling.nami)
summary(sim.out.nami.ru)
#0.03067368 (-0.01596302 0.07764955)


#plot(density(sim.out.nami.ru$qi$fd), main="First Difference -- Ruling Party -- Namibia", xlab="Change in Likelihood of Ethnic Id", col="blueviolet", lwd=2.1, xlim=c(-.3,.2))
#abline(v=0, col="azure4", lty="dashed", lwd=.8)



############## NIGERIA #####################

nige <- subset(ruldataom, DumNi1==1 |DumNi15==1| DumNi2==1)
unique(nige$region)

nige$reg2[nige$region==2] <- 1
nige$reg2[nige$region!=2] <- 0
nige$reg3[nige$region==3] <- 1
nige$reg3[nige$region!=3] <- 0
nige$reg4[nige$region==4] <- 1
nige$reg4[nige$region!=4] <- 0
nige$reg5[nige$region==5] <- 1
nige$reg5[nige$region!=5] <- 0
nige$reg6[nige$region==6] <- 1
nige$reg6[nige$region!=6] <- 0
nige$reg7[nige$region==7] <- 1
nige$reg7[nige$region!=7] <- 0
nige$reg8[nige$region==8] <- 1
nige$reg8[nige$region!=8] <- 0

pre.nige<-imbalance(group=nige$ruling_party,data=nige, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.nige

match.nige<- matchit(ruling_party ~ lang_d_10 + reg2 + reg3 + reg4 + reg5 + reg6 + reg7 +reg8+ DumNi1 +DumNi15+ urban + age + male + educ_basic+radio_daily, data = nige, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.nige)

nige.data <- match.data(match.nige)

post.nige.bal <- imbalance(group= nige.data$ruling_party, data=nige.data, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2",  "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.nige.bal


nige.model <- zelig(identity_ethnic_1 ~ ruling_party + lang_d_10 + reg2 + reg3 + reg4+reg5+ reg6+ reg7+reg8+urban + age + male + educ_basic + DumNi1+DumNi15 +radio_daily, data = nige.data, model = "logit")
summary(nige.model)

set.seed(02138)
oppo.nige<-setx(nige.model, ruling_party=0)
ruling.nige <- setx(nige.model, ruling_party=1)
sim.out.nige.ru <- sim(nige.model, x = oppo.nige, x1 = ruling.nige)
summary(sim.out.nige.ru)
#-0.02987543 (-0.05664257 -0.003095715)

#plot(density(sim.out.nige.ru$qi$fd), main="First Difference -- Ruling Party -- Nigeria", xlab="Change in Likelihood of Ethnic Id", col="darkgoldenrod", lwd=2.1, xlim=c(-.3,.2))
#abline(v=0, col="azure4", lty="dashed", lwd=.8)


################### SOUTH AFRICA ###########

sout <- subset(ruldataom, DumSo1==1 | DumSo2==1)
unique(sout$region)

sout$reg2[sout$region==2] <- 1
sout$reg2[sout$region!=2] <- 0
sout$reg3[sout$region==3] <- 1
sout$reg3[sout$region!=3] <- 0
sout$reg4[sout$region==4] <- 1
sout$reg4[sout$region!=4] <- 0
sout$reg5[sout$region==5] <- 1
sout$reg5[sout$region!=5] <- 0
sout$reg6[sout$region==6] <- 1
sout$reg6[sout$region!=6] <- 0
sout$reg7[sout$region==7] <- 1
sout$reg7[sout$region!=7] <- 0
sout$reg8[sout$region==8] <- 1
sout$reg8[sout$region!=8] <- 0
sout$reg9[sout$region==9] <- 1
sout$reg9[sout$region!=9] <- 0

pre.sout<-imbalance(group=sout$ruling_party,data=sout, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.sout

match.sout<- matchit(ruling_party ~ lang_d_10 + reg2 + reg3 + reg4 + reg5 + reg6 + reg7 +reg8+ reg9+DumSo1+ urban + age + male + educ_basic+radio_daily, data = sout, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.sout)

sout.data <- match.data(match.sout)

post.sout.bal <- imbalance(group= sout.data$ruling_party, data=sout.data, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.sout.bal

sout.model <- zelig(identity_ethnic_1 ~ ruling_party + lang_d_10 + reg2 + reg3 + reg4+reg5+ reg6+ reg7+reg8+reg9+urban + age + male + educ_basic + DumSo1+radio_daily, data = sout.data, model = "logit")
summary(sout.model)

set.seed(02138)
oppo.sout<-setx(sout.model, ruling_party=0)
ruling.sout <- setx(sout.model, ruling_party=1)
sim.out.sout.ru <- sim(sout.model, x = oppo.sout, x1 = ruling.sout)
summary(sim.out.sout.ru)
#0.008145427 (-0.02489351 0.04190211)

#plot(density(sim.out.sout.ru$qi$fd), main="First Difference -- Ruling Party -- South Africa", xlab="Change in Likelihood of Ethnic Id", col="darkseagreen4", lwd=2.1, xlim=c(-.3,.2))
#abline(v=0, col="azure4", lty="dashed", lwd=.8)

################## TANZANIA #############
##### Because we find no matches in region 10, we drop that region from the analysis  ######


tanz <- subset(ruldataom, DumTa1==1 | DumTa2==1)
unique(tanz$region)

tanz$reg2[tanz$region==2] <- 1
tanz$reg2[tanz$region!=2] <- 0
tanz$reg3[tanz$region==3] <- 1
tanz$reg3[tanz$region!=3] <- 0
tanz$reg4[tanz$region==4] <- 1
tanz$reg4[tanz$region!=4] <- 0
tanz$reg5[tanz$region==5] <- 1
tanz$reg5[tanz$region!=5] <- 0
tanz$reg6[tanz$region==6] <- 1
tanz$reg6[tanz$region!=6] <- 0
tanz$reg7[tanz$region==7] <- 1
tanz$reg7[tanz$region!=7] <- 0
tanz$reg8[tanz$region==8] <- 1
tanz$reg8[tanz$region!=8] <- 0
tanz$reg9[tanz$region==9] <- 1
tanz$reg9[tanz$region!=9] <- 0
tanz$reg10[tanz$region==10] <- 1
tanz$reg10[tanz$region!=10] <- 0
tanz$reg11[tanz$region==11] <- 1
tanz$reg11[tanz$region!=11] <- 0
tanz$reg12[tanz$region==12] <- 1
tanz$reg12[tanz$region!=12] <- 0


pre.tanz<-imbalance(group=tanz$ruling_party,data=tanz, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.tanz

match.tanz<- matchit(ruling_party ~ lang_d_10 + reg2 + reg3 + reg4 + reg5 + reg6 + reg7 +reg8+reg9+reg11+reg12+DumTa1+ urban + age + male + educ_basic+radio_daily, data = tanz, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.tanz)

tanz.data <- match.data(match.tanz)

post.tanz.bal <- imbalance(group= tanz.data$ruling_party, data=tanz.data, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2", "DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.tanz.bal

tanz.model <- zelig(identity_ethnic_1 ~ ruling_party + lang_d_10 + reg2 + reg3 + reg4+reg5+ reg6+ reg7+reg8+reg9+reg11+reg12+urban + age + male + educ_basic + DumTa1+radio_daily, data = tanz.data, model = "logit")
summary(tanz.model)

set.seed(02138)
oppo.tanz<-setx(tanz.model, ruling_party=0)
ruling.tanz <- setx(tanz.model, ruling_party=1)
sim.out.tanz.ru <- sim(tanz.model, x = oppo.tanz, x1 = ruling.tanz)
summary(sim.out.tanz.ru)
#0.01501517 (-0.004684594 0.03432708)

#plot(density(sim.out.tanz$qi$fd.ru), main="First Difference -- Ruling Party -- Tanzania", xlab="Change in Likelihood of Ethnic Id", col="darkorange4", lwd=2.1, xlim=c(-.3,.2))
#abline(v=0, col="azure4", lty="dashed", lwd=.8)

############ UGANDA ############################

ugan <- subset(ruldataom, DumUg1==1 | DumUg2==1)
unique(ugan$region)

ugan$reg2[ugan$region==2] <- 1
ugan$reg2[ugan$region!=2] <- 0
ugan$reg3[ugan$region==3] <- 1
ugan$reg3[ugan$region!=3] <- 0
ugan$reg4[ugan$region==4] <- 1
ugan$reg4[ugan$region!=4] <- 0

pre.ugan<-imbalance(group=ugan$ruling_party,data=ugan, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa1", "DumTa2", "DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.ugan

match.ugan<- matchit(ruling_party ~ lang_d_10 + reg2 + reg3 + reg4 + DumUg1+ urban + age + male + educ_basic+ radio_daily, data = ugan, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa1","DumTa2", "DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.ugan)

ugan.data <- match.data(match.ugan)

post.ugan.bal <- imbalance(group= ugan.data$ruling_party, data=ugan.data, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2","DumTa1", "DumTa2", "DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.ugan.bal

ugan.model <- zelig(identity_ethnic_1 ~ ruling_party + lang_d_10 + reg2 +reg3 + reg4+urban + age + male + educ_basic + DumUg1+radio_daily, data = ugan.data, model = "logit")
summary(ugan.model)

set.seed(02138)
oppo.ugan<-setx(ugan.model, ruling_party=0)
ruling.ugan <- setx(ugan.model, ruling_party=1)
sim.out.ugan.ru <- sim(ugan.model, x = oppo.ugan, x1 = ruling.ugan)
summary(sim.out.ugan.ru)
#0.006176555 (-0.01675064 0.03131278)


#plot(density(sim.out.ugan.ru$qi$fd), main="First Difference -- Ruling Party -- Uganda", xlab="Change in Likelihood of Ethnic Id", col="dodgerblue2", lwd=2.1, xlim=c(-.3,.2))
#abline(v=0, col="azure4", lty="dashed", lwd=.8)


############## Zambia ##########################

zamb <- subset(ruldataom, DumZa1==1 | DumZa2==1)
unique(zamb$region)

zamb$reg2[zamb$region==2] <- 1
zamb$reg2[zamb$region!=2] <- 0
zamb$reg3[zamb$region==3] <- 1
zamb$reg3[zamb$region!=3] <- 0
zamb$reg4[zamb$region==4] <- 1
zamb$reg4[zamb$region!=4] <- 0
zamb$reg5[zamb$region==5] <- 1
zamb$reg5[zamb$region!=5] <- 0
zamb$reg6[zamb$region==6] <- 1
zamb$reg6[zamb$region!=6] <- 0
zamb$reg7[zamb$region==7] <- 1
zamb$reg7[zamb$region!=7] <- 0

pre.zamb<-imbalance(group=zamb$ruling_party,data=zamb, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa1", "DumTa2", "DumUg1","DumUg2", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.zamb


match.zamb<- matchit(ruling_party ~ lang_d_10 + reg2 + reg3 + reg4 + reg5+reg6+reg7+DumZa1+ urban + age + male + educ_basic+radio_daily, data = zamb, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa1","DumTa2", "DumUg1", "DumUg2", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.zamb)

zamb.data <- match.data(match.zamb)

post.zamb.bal <- imbalance(group= zamb.data$ruling_party, data=zamb.data, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2","DumTa1", "DumTa2","DumUg1", "DumUg2", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.zamb.bal

zamb.model <- zelig(identity_ethnic_1 ~ ruling_party + lang_d_10 + reg2 +reg3 + reg4+ reg5+reg6+reg7+urban + age + male + educ_basic + DumZa1+radio_daily, data = zamb.data, model = "logit")
summary(zamb.model)

set.seed(02138)
oppo.zamb<-setx(zamb.model, ruling_party=0)
ruling.zamb <- setx(zamb.model, ruling_party=1)
sim.out.zamb.ru <- sim(zamb.model, x = oppo.zamb, x1 = ruling.zamb)
summary(sim.out.zamb.ru)
#0.003093406 (-0.02730832 0.04184049)


#plot(density(sim.out.zamb.ru$qi$fd), main="First Difference -- Ruling Party -- Zambia", xlab="Change in Likelihood of Ethnic Id", col="darkslategray4", lwd=2.1, xlim=c(-.3,.2))
#abline(v=0, col="azure4", lty="dashed", lwd=.8)



################# ZIMBABWE ###########################
zimb <- subset(ruldataom, DumZi1==1 | DumZi2==1)
unique(zimb$region)

zimb$reg2[zimb$region==2] <- 1
zimb$reg2[zimb$region!=2] <- 0
zimb$reg3[zimb$region==3] <- 1
zimb$reg3[zimb$region!=3] <- 0
zimb$reg4[zimb$region==4] <- 1
zimb$reg4[zimb$region!=4] <- 0
zimb$reg5[zimb$region==5] <- 1
zimb$reg5[zimb$region!=5] <- 0
zimb$reg6[zimb$region==6] <- 1
zimb$reg6[zimb$region!=6] <- 0
zimb$reg7[zimb$region==7] <- 1
zimb$reg7[zimb$region!=7] <- 0


pre.zimb<-imbalance(group=zimb$ruling_party,data=zimb, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa1", "DumTa2", "DumUg1","DumUg2","DumZa1", "DumZa2", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.zimb

match.zimb<- matchit(ruling_party ~ lang_d_10 + reg2 + reg3 + reg4 + reg5+reg6+reg7+DumZi1+ urban + age + male + educ_basic+radio_daily, data = zimb, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa1","DumTa2", "DumUg1", "DumUg2", "DumZa1","DumZa2",  "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.zimb)

zimb.data <- match.data(match.zimb)

post.zimb.bal <- imbalance(group= zimb.data$ruling_party, data=zimb.data, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2","DumTa1", "DumTa2","DumUg1", "DumUg2","DumZa1",  "DumZa2", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.zimb.bal

zimb.model <- zelig(identity_ethnic_1 ~ ruling_party + lang_d_10 + reg2 +reg3 + reg4+ reg5+reg6+reg7+urban + age + male + educ_basic + DumZi1+radio_daily, data = zimb.data, model = "logit")
summary(zimb.model)

set.seed(02138)
oppo.zimb<-setx(zimb.model, ruling_party=0)
ruling.zimb <- setx(zimb.model, ruling_party=1)
sim.out.zimb.ru <- sim(zimb.model, x = oppo.zimb, x1 = ruling.zimb)
summary(sim.out.zimb.ru)
#0.02492152 (-0.02458328 0.07044253)



#plot(density(sim.out.zimb.ru$qi$fd), main="First Difference -- Ruling Party -- Zimbabwe", xlab="Change in Likelihood of Ethnic Id", col="deeppink3", lwd=2.1, xlim=c(-.3,.2))
#abline(v=0, col="azure4", lty="dashed", lwd=.8)



####### Ghana ######
### LANG D 10 not coded, removed


ghan <- subset(ruldataom, DumGa2==1)
unique(ghan$region)

ghan$reg2[ghan$region==2] <- 1
ghan$reg2[ghan$region!=2] <- 0
ghan$reg3[ghan$region==3] <- 1
ghan$reg3[ghan$region!=3] <- 0
ghan$reg4[ghan$region==4] <- 1
ghan$reg4[ghan$region!=4] <- 0
ghan$reg5[ghan$region==5] <- 1
ghan$reg5[ghan$region!=5] <- 0
ghan$reg6[ghan$region==6] <- 1
ghan$reg6[ghan$region!=6] <- 0
ghan$reg7[ghan$region==7] <- 1
ghan$reg7[ghan$region!=7] <- 0
ghan$reg8[ghan$region==8] <- 1
ghan$reg8[ghan$region!=8] <- 0

table(ghan$ruling_party)

pre.ghan<-imbalance(group=ghan$ruling_party, data=ghan, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa1", "DumTa2", "DumUg1","DumUg2","DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.ghan

match.ghan<- matchit(ruling_party ~ reg2 + reg3 + reg4 + reg5+reg6+reg7+reg8+ urban + age + male + educ_basic+radio_daily, data = ghan, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa1","DumTa2", "DumUg1", "DumUg2", "DumZa1","DumZa2",  "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.ghan)

ghan.data <- match.data(match.ghan)

post.ghan.bal <- imbalance(group= ghan.data$ruling_party, data=ghan.data, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2","DumTa1", "DumTa2","DumUg1", "DumUg2","DumZa1", "DumZi1", "DumZa2", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.ghan.bal

ghan.model <- zelig(identity_ethnic_1 ~ ruling_party + reg2 +reg3 + reg4+ reg5+reg6+reg7+reg8+urban + age + male + educ_basic +radio_daily, data = ghan.data, model = "logit")
summary(ghan.model)

set.seed(02138)
oppo.ghan<-setx(ghan.model, ruling_party=0)
int.ghan <- setx(ghan.model, ruling_party=1)
sim.out.ghan.ru <- sim(ghan.model, x = oppo.ghan, x1 = int.ghan)
summary(sim.out.ghan.ru)
#0.01937382 (-0.05068284 0.08683396)




######## Kenya #########
## LANG D 10 not defined, dropped


ken <- subset(ruldataom, DumKe2==1)
unique(ken$region)

ken$reg2[ken$region==2] <- 1
ken$reg2[ken$region!=2] <- 0
ken$reg3[ken$region==3] <- 1
ken$reg3[ken$region!=3] <- 0
ken$reg4[ken$region==4] <- 1
ken$reg4[ken$region!=4] <- 0
ken$reg5[ken$region==5] <- 1
ken$reg5[ken$region!=5] <- 0
ken$reg6[ken$region==6] <- 1
ken$reg6[ken$region!=6] <- 0
ken$reg7[ken$region==7] <- 1
ken$reg7[ken$region!=7] <- 0
ken$reg8[ken$region==8] <- 1
ken$reg8[ken$region!=8] <- 0

table(ken$ruling_party)

pre.ken<-imbalance(group=ken$ruling_party, data=ken, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa1", "DumTa2", "DumUg1","DumUg2","DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.ken

match.ken<- matchit(ruling_party ~ reg2 + reg3 + reg4 + reg5+reg6+reg7+reg8+ urban + age + male + educ_basic+radio_daily, data = ken, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa1","DumTa2", "DumUg1", "DumUg2", "DumZa1","DumZa2",  "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.ken)

ken.data <- match.data(match.ken)

post.ken.bal <- imbalance(group= ken.data$ruling_party, data=ken.data, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2","DumTa1", "DumTa2","DumUg1", "DumUg2","DumZa1", "DumZi1", "DumZa2", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.ken.bal

ken.model <- zelig(identity_ethnic_1 ~ ruling_party + reg2 +reg3 + reg4+ reg5+reg6+reg7+reg8+urban + age + male + educ_basic +radio_daily, data = ken.data, model = "logit")
summary(ken.model)


set.seed(02138)
oppo.ken<-setx(ken.model, ruling_party=0)
int.ken <- setx(ken.model, ruling_party=1)
sim.out.ken.ru <- sim(ken.model, x = oppo.ken, x1 = int.ken)
summary(sim.out.ken.ru)


####### Mozambique ########
#We find no matches in regions 2 and 4 and drop them from the analysis
#no data on lang_d_10, dropped 


moz <- subset(ruldataom, DumMo2==1)
unique(moz$region)

moz$reg2[moz$region==2] <- 1
moz$reg2[moz$region!=2] <- 0
moz$reg3[moz$region==3] <- 1
moz$reg3[moz$region!=3] <- 0
moz$reg4[moz$region==4] <- 1
moz$reg4[moz$region!=4] <- 0
moz$reg5[moz$region==5] <- 1
moz$reg5[moz$region!=5] <- 0
moz$reg6[moz$region==6] <- 1
moz$reg6[moz$region!=6] <- 0
moz$reg7[moz$region==7] <- 1
moz$reg7[moz$region!=7] <- 0
moz$reg8[moz$region==8] <- 1
moz$reg8[moz$region!=8] <- 0
moz$reg9[moz$region==9] <- 1
moz$reg9[moz$region!=9] <- 0
moz$reg10[moz$region==10] <- 1
moz$reg10[moz$region!=10] <- 0
moz$reg11[moz$region==11] <- 1
moz$reg11[moz$region!=11] <- 0

table(moz$ruling_party)

pre.moz<-imbalance(group=moz$ruling_party, data=moz, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa1", "DumTa2", "DumUg1","DumUg2","DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.moz

match.moz<- matchit(ruling_party ~ reg3  + reg5+reg6+reg7+reg8+ urban + age + male + educ_basic+radio_daily, data = moz, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa1","DumTa2", "DumUg1", "DumUg2", "DumZa1","DumZa2",  "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.moz)

moz.data <- match.data(match.moz)

post.moz.bal <- imbalance(group= moz.data$ruling_party, data=moz.data, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2","DumTa1", "DumTa2","DumUg1", "DumUg2","DumZa1", "DumZi1", "DumZa2", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.moz.bal

moz.model <- zelig(identity_ethnic_1 ~ ruling_party + reg3 + reg5+reg6+reg7+reg8+urban + age + male + educ_basic +radio_daily, data = moz.data, model = "logit")
summary(moz.model)

set.seed(02138)
oppo.moz<-setx(moz.model, ruling_party=0)
int.moz <- setx(moz.model, ruling_party=1)
sim.out.moz.ru <- sim(moz.model, x = oppo.moz, x1 = int.moz)
summary(sim.out.moz.ru)
#-0.04108726 (-0.09450516 0.01211003)


###### Senegal ##########
#lang dropped again, not available

sene <- subset(ruldataom, DumSen2==1)
unique(sene$region)

sene$reg2[sene$region==2] <- 1
sene$reg2[sene$region!=2] <- 0
sene$reg3[sene$region==3] <- 1
sene$reg3[sene$region!=3] <- 0
sene$reg4[sene$region==4] <- 1
sene$reg4[sene$region!=4] <- 0
sene$reg5[sene$region==5] <- 1
sene$reg5[sene$region!=5] <- 0
sene$reg6[sene$region==6] <- 1
sene$reg6[sene$region!=6] <- 0


table(sene$ruling_party)

pre.sene<-imbalance(group=sene$ruling_party, data=sene, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa1", "DumTa2", "DumUg1","DumUg2","DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.sene

match.sene<- matchit(ruling_party ~ reg2 + reg3 + reg4 + reg5+reg6+ urban + age + male + educ_basic+radio_daily, data = sene, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa1","DumTa2", "DumUg1", "DumUg2", "DumZa1","DumZa2",  "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.sene)

sene.data <- match.data(match.sene)

post.sene.bal <- imbalance(group= sene.data$ruling_party, data=sene.data, drop = c("ruling_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2","DumTa1", "DumTa2","DumUg1", "DumUg2","DumZa1", "DumZi1", "DumZa2", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.sene.bal

sene.model <- zelig(identity_ethnic_1 ~ ruling_party + reg2 +reg3 + reg4 + reg5 + reg6 +urban + age + male + educ_basic +radio_daily, data = sene.data, model = "logit")
summary(sene.model)

set.seed(02138)
oppo.sene<-setx(sene.model, ruling_party=0)
int.sene <- setx(sene.model, ruling_party=1)
sim.out.sene.ru <- sim(sene.model, x = oppo.sene, x1 = int.sene)
summary(sim.out.sene.ru)
#0.03069964 (-0.03131698 0.09287556)





############# CROSS NATIONAL ANALYSIS ##########################
############### RULING PARTY SUPPORT ON ETHNIC ID ###############


cross.national <- rbind(zimb.data[,c(1:38, 45:47)], zamb.data[, c(1:38, 45:47)], ugan.data[, c(1:38, 42:44)], tanz.data[, c(1:38, 50:52)], sout.data[, c(1:38, 47:49)], nige.data[ , c(1:38, 46:48)], nami.data[, c(1:38, 48:50)], mala.data[ , c(1:38, 41:43)], bots.data[ , c(1:38, 42:44)], ghan.data[ , c(1:38, 46:48)], ken.data[ , c(1:38, 46:48)], sene.data[ , c(1:38, 44:46)], moz.data[ , c(1:38, 49:51)])
dim(cross.national)
#[1] [1] 22393    41

cross.national$nation.wts[cross.national$DumBo1==1|cross.national$DumBo2==1] <- 1 / nrow(bots.data)
cross.national$nation.wts[cross.national$DumMa1==1|cross.national$DumMa2==1] <- 1 / nrow(mala.data)
#cross.national$nation.wts[cross.national$DumMali1==1|cross.national$DumMali2==1] <- 1 / nrow(mali.data)
cross.national$nation.wts[cross.national$DumNa1==1|cross.national$DumNa2==1|cross.national$DumNa15==1] <- 1 / nrow(nami.data)
cross.national$nation.wts[cross.national$DumNi1==1|cross.national$DumNi2==1|cross.national$DumNi15==1] <- 1 / nrow(nige.data)
cross.national$nation.wts[cross.national$DumSo1==1|cross.national$DumSo2==1] <- 1 / nrow(sout.data)
cross.national$nation.wts[cross.national$DumTa1==1|cross.national$DumTa2==1] <- 1 / nrow(tanz.data)
cross.national$nation.wts[cross.national$DumUg1==1|cross.national$DumUg2==1] <- 1 / nrow(ugan.data)
cross.national$nation.wts[cross.national$DumZa1==1|cross.national$DumZa2==1] <- 1 / nrow(zamb.data)
cross.national$nation.wts[cross.national$DumZi1==1|cross.national$DumZi2==1] <- 1 / nrow(zimb.data)
cross.national$nation.wts[cross.national$DumGa2==1] <- 1 / nrow(ghan.data)
cross.national$nation.wts[cross.national$DumKe2==1] <- 1 / nrow(ken.data)
cross.national$nation.wts[cross.national$DumMo2==1] <- 1 / nrow(moz.data)
cross.national$nation.wts[cross.national$DumSen2==1] <- 1 / nrow(sene.data)

#real weights is country weights * nation weights
cross.national$real.weights <- cross.national$weights * cross.national$nation.wts * 1000

#make round/country dummies separately
cross.national$bots[cross.national$DumBo1==1|cross.national$DumBo2==1] <- 1
cross.national$bots[cross.national$DumBo1!=1&cross.national$DumBo2!=1] <- 0
cross.national$mala[cross.national$DumMa1==1|cross.national$DumMa2==1] <- 1
cross.national$mala[cross.national$DumMa1!=1&cross.national$DumMa2!=1] <- 0
#cross.national$mali[cross.national$DumMali1==1|cross.national$DumMali2==1] <- 1
#cross.national$mali[cross.national$DumMali1!=1&cross.national$DumMali2!=1] <- 0
cross.national$nami[cross.national$DumNa1==1|cross.national$DumNa2==1|cross.national$DumNa15==1] <- 1
cross.national$nami[cross.national$DumNa1!=1&cross.national$DumNa2!=1&cross.national$DumNa15!=1] <- 0
cross.national$nige[cross.national$DumNi1==1|cross.national$DumNi2==1|cross.national$DumNi15==1] <- 1
cross.national$nige[cross.national$DumNi1!=1&cross.national$DumNi2!=1&cross.national$DumNi15!=1] <- 0
cross.national$sout[cross.national$DumSo1==1|cross.national$DumSo2==1] <- 1 
cross.national$sout[cross.national$DumSo1!=1&cross.national$DumSo2!=1] <- 0
cross.national$tanz[cross.national$DumTa1==1|cross.national$DumTa2==1] <- 1
cross.national$tanz[cross.national$DumTa1!=1&cross.national$DumTa2!=1] <- 0
cross.national$ugan[cross.national$DumUg1==1|cross.national$DumUg2==1] <- 1
cross.national$ugan[cross.national$DumUg1!=1&cross.national$DumUg2!=1] <- 0
cross.national$zamb[cross.national$DumZa1==1|cross.national$DumZa2==1] <- 1
cross.national$zamb[cross.national$DumZa1!=1&cross.national$DumZa2!=1] <- 0
cross.national$zimb[cross.national$DumZi1==1|cross.national$DumZi2==1] <- 1
cross.national$zimb[cross.national$DumZi1!=1&cross.national$DumZi2!=1] <- 0

cross.national$r15[cross.national$DumNa15==1|cross.national$DumNi15==1] <- 1
cross.national$r15[cross.national$DumNa15!=1&cross.national$DumNi15!=1] <- 0
cross.national$r2[cross.national$DumBo2==1|cross.national$DumMa2==1|cross.national$DumNa2==1|cross.national$DumNi2==1|cross.national$DumSo2==1|cross.national$DumTa2==1|cross.national$DumUg2==1|cross.national$DumZa2==1|cross.national$DumZi2==1] <- 1
cross.national$r2[cross.national$DumBo2!=1&cross.national$DumMa2!=1&cross.national$DumNa2!=1&cross.national$DumNi2!=1&cross.national$DumSo2!=1&cross.national$DumTa2!=1&cross.national$DumUg2!=1&cross.national$DumZa2!=1&cross.national$DumZi2!=1] <- 0

dim(cross.national)
names(cross.national)

## can't do country fixed effects when you include the additional countries, but data is already balanced within countries, weighted by country size (so each contributes equally)
## will also run without last 3 countries


######## Table 4, Model 1 (with all countries)############
### just matching vars
#lang is matched on in some subsets, but dropped because of the 0's in other countries

model1 <- zelig(identity_ethnic_1 ~ ruling_party + urban + age + male + educ_basic + radio_daily + r2 +r15, data = cross.national, model = "logit", weights = cross.national$real.weights)
summary(model1)


not.opp <- setx(model1, ruling_party =0)
yes.opp <- setx(model1, ruling_party =1)
sim.out.cross.nat <- sim(model1, x=not.opp, x1=yes.opp)
summary(sim.out.cross.nat)

####### Table 4, Model 2 #########
### with elec_prox elec_comp prox_comp

model2 <- zelig(identity_ethnic_1 ~ ruling_party + elec_prox + elec_comp + prox_comp + urban + age + male + educ_basic + radio_daily + r2, data = cross.national, model = "logit", weights = cross.national$real.weights)
summary(model2)


not.opp <- setx(model2, ruling_party =0)
yes.opp <- setx(model2, ruling_party =1)
sim.out.cross.nat.2 <- sim(model2, x=not.opp, x1=yes.opp)
summary(sim.out.cross.nat.2)


########### Data without Ghana, Kenya, Mozambique and Senegal ###########
cross.national.abbrev <- subset(cross.national, DumGa2!=1&DumKe2!=1&DumMo2!=1&DumSen2!=1)

## MODEL 1, with fixed effects
#model1 <- zelig(identity_ethnic_1 ~ opposition_party + urban + age + male + educ_basic + radio_daily + mala + nami + nige + sout + tanz + ugan + zamb + zimb + r2, data = cross.national.abbrev, model = "logit", weights = cross.national.abbrev$real.weights)
#summary(model1)
#
#model1a <- zelig(identity_ethnic_1 ~ opposition_party + I(opposition_party*mala) + I(opposition_party*nami) + I(opposition_party*nige) + I(opposition_party*sout) + I(opposition_party*tanz) + I(opposition_party*ugan) + I(opposition_party*zamb) + I(opposition_party*zimb) + urban + age + male + educ_basic + radio_daily + mala + nami + nige + sout + tanz + ugan + zamb + zimb + r2, data = cross.national.abbrev, model = "logit", weights = cross.national.abbrev$real.weights)
#summary(model1a)

#############################
### Table 4, Model 3 ##########
## with elec_prox elec_comp prox_comp

model2 <- zelig(identity_ethnic_1 ~ ruling_party + elec_prox + elec_comp + prox_comp + urban + age + male + educ_basic + radio_daily + mala + nami + nige + sout + tanz + ugan + zamb + zimb + r15 + r2, data = cross.national.abbrev, model = "logit", weights = cross.national.abbrev$real.weights)
summary(model2)

not.opp <- setx(model2, ruling_party =0)
yes.opp <- setx(model2, ruling_party =1)
sim.out.cross.nat.2 <- sim(model2, x=not.opp, x1=yes.opp)
summary(sim.out.cross.nat.2)





############################################## SECTION FOUR ####################################
############### Opposition Support
### Do people who support the opposition identity ethnically more often?
## NB: Will overwrite Section Three Analysis
################################################################################################


ruldata<-data[, c("identity_ethnic_1", "opposition_party", "lang_d_10", "radio_daily", "region", "urban", "age", "male", "educ_basic", "elec_prox", "elec_comp", "prox_comp","DumBo1","DumBo2","DumMa1", "DumMa2", "DumMali1", "DumMali2", "DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2")]

###omitting data-
nrow(ruldata)
ruldataom<-na.omit(ruldata)
nrow(ruldataom)

############# Country by Country matching ######################


### Botswana ####

bots <- subset(ruldataom, DumBo1==1 | DumBo2==1)
bots$reg2[bots$region==2] <- 1
bots$reg2[bots$region!=2] <- 0
bots$reg3[bots$region==3] <- 1
bots$reg3[bots$region!=3] <- 0
bots$reg4[bots$region==4] <- 1
bots$reg4[bots$region!=4] <- 0

pre.bots<-imbalance(group=bots$opposition_party,data=bots, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo2", "DumMa1", "DumMa2", "DumMali1", "DumMali2", "DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.bots

agecut <- c(25, 55)
mycp <- list(age=agecut)

match.bots <- matchit(opposition_party ~ lang_d_10 + reg2 + reg3 + reg4 + DumBo1 + urban + age + male + educ_basic + radio_daily, data = bots, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo2", "DumMa1", "DumMa2", "DumMali1", "DumMali2", "DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.bots)

bots.data <- match.data(match.bots)

post.bots.bal <- imbalance(group= bots.data$opposition_party, data=bots.data, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo2", "DumMa1", "DumMa2", "DumMali1", "DumMali2", "DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.bots.bal

bots.model <- zelig(identity_ethnic_1 ~ opposition_party + lang_d_10 + reg2 + reg3 + reg4 + urban + age + male + educ_basic + radio_daily + DumBo1 , data = bots.data, model = "logit")
summary(bots.model)

set.seed(02138)
notopp.bots <-setx(bots.model, opposition_party=0)
opp.bots <- setx(bots.model, opposition_party=1)
sim.out.bots.opp <- sim(bots.model, x = notopp.bots, x1 = opp.bots)
summary(sim.out.bots.opp)
# Change:
#0.01886481 (-0.04616518 0.08086091)

#plot(density(sim.out.bots.int$qi$fd), main="First Difference -- Ruling Party -- Botswana", xlab="Change in Likelihood of Ethnic Id", col="coral2", lwd=2.1)
#abline(v=0, col="azure4", lty="dashed", lwd=.8)



############ Malawi ################
mala <- subset(ruldataom, DumMa1==1 | DumMa2==1)
unique(mala$region)
mala$reg2[mala$region==2] <- 1
mala$reg2[mala$region!=2] <- 0
mala$reg3[mala$region==3] <- 1
mala$reg3[mala$region!=3] <- 0

pre.mala<-imbalance(group=mala$opposition_party,data=mala, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa2", "DumMali1", "DumMali2", "DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.mala

match.mala <- matchit(opposition_party ~ lang_d_10 + reg2 + reg3 + DumMa1 + urban + age + male+ educ_basic + radio_daily, data = mala, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa2", "DumMali1", "DumMali2", "DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.mala)

mala.data <- match.data(match.mala)

post.mala.bal <- imbalance(group= mala.data$opposition_party, data=mala.data, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa2", "DumMali1", "DumMali2", "DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"), weights=mala.data$weights)
post.mala.bal

mala.model <- zelig(identity_ethnic_1 ~ opposition_party + lang_d_10 + reg2 + reg3 + urban + age + male + educ_basic +radio_daily + DumMa1, data = mala.data, model = "logit")
summary(mala.model)

set.seed(02138)
notopp.mala<-setx(mala.model, opposition_party=0)
opp.mala <- setx(mala.model, opposition_party=1)
sim.out.mala.opp <- sim(mala.model, x = notopp.mala, x1 = opp.mala)
summary(sim.out.mala.opp)
#0.0516794 (0.004821056 0.1013835)

#plot(density(sim.out.mala.int$qi$fd), main="Malawi", xlab="Change in Likelihood of Ethnic Id", #col="cadetblue",xlim=c(-.3,.2), lwd=2.1)
#abline(v=0, col="azure4", lty="dashed", lwd=.8)



######### NAMIBIA ###########

#round 1.5 not included

nami <- subset(ruldataom, DumNa1==1 |DumNa2==1|DumNa15==1)
unique(nami$region)
nrow(nami)

nami$reg2[nami$region==2] <- 1
nami$reg2[nami$region!=2] <- 0
nami$reg3[nami$region==3] <- 1
nami$reg3[nami$region!=3] <- 0
nami$reg4[nami$region==4] <- 1
nami$reg4[nami$region!=4] <- 0
nami$reg5[nami$region==5] <- 1
nami$reg5[nami$region!=5] <- 0
nami$reg6[nami$region==6] <- 1
nami$reg6[nami$region!=6] <- 0
nami$reg7[nami$region==7] <- 1
nami$reg7[nami$region!=7] <- 0
nami$reg8[nami$region==8] <- 1
nami$reg8[nami$region!=8] <- 0
nami$reg9[nami$region==9] <- 1
nami$reg9[nami$region!=9] <- 0
nami$reg10[nami$region==10] <- 1
nami$reg10[nami$region!=10] <- 0

pre.nami<-imbalance(group=nami$opposition_party,data=nami, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.nami

match.nami<- matchit(opposition_party ~ lang_d_10 + reg2 + reg3 + reg4 + reg5 + reg6 + reg7 +reg8+reg9+reg10+ DumNa1 +DumNa15 + urban + age + male + educ_basic+radio_daily, data = nami, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.nami)

nami.data <- match.data(match.nami)

post.nami.bal <- imbalance(group= nami.data$opposition_party, data=nami.data, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2", "DumNa15","DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.nami.bal

nami.model <- zelig(identity_ethnic_1 ~ opposition_party + lang_d_10 + reg2 + reg3 + reg4+reg5+ reg6+ reg7+reg8+reg9+reg10+urban + age + male + educ_basic + DumNa1 + DumNa15 +radio_daily , data = nami.data, model = "logit")
summary(nami.model)


set.seed(02138)
notopp.nami<-setx(nami.model, opposition_party=0)
opp.nami <- setx(nami.model, opposition_party=1)
sim.out.nami.opp <- sim(nami.model, x = notopp.nami, x1 = opp.nami)
summary(sim.out.nami.opp)
#0.0347262 (-0.03129101 0.1042392)
#0.007594674 0.02820800 -0.0457841 0.06429955


#plot(density(sim.out.nami.int$qi$fd), main="First Difference -- Ruling Party -- Namibia", xlab="Change in Likelihood of Ethnic Id", col="blueviolet", lwd=2.1, xlim=c(-.3,.2))
#abline(v=0, col="azure4", lty="dashed", lwd=.8)



############## NIGERIA #####################

nige <- subset(ruldataom, DumNi1==1| DumNi2==1|DumNi15==1)
unique(nige$region)

nige$reg2[nige$region==2] <- 1
nige$reg2[nige$region!=2] <- 0
nige$reg3[nige$region==3] <- 1
nige$reg3[nige$region!=3] <- 0
nige$reg4[nige$region==4] <- 1
nige$reg4[nige$region!=4] <- 0
nige$reg5[nige$region==5] <- 1
nige$reg5[nige$region!=5] <- 0
nige$reg6[nige$region==6] <- 1
nige$reg6[nige$region!=6] <- 0
nige$reg7[nige$region==7] <- 1
nige$reg7[nige$region!=7] <- 0
nige$reg8[nige$region==8] <- 1
nige$reg8[nige$region!=8] <- 0

pre.nige<-imbalance(group=nige$opposition_party,data=nige, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.nige

match.nige<- matchit(opposition_party ~ lang_d_10 + reg2 + reg3 + reg4 + reg5 + reg6 + reg7 + reg8 + DumNi1 + DumNi15 + urban + age + male + educ_basic+radio_daily, data = nige, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.nige)

nige.data <- match.data(match.nige)

post.nige.bal <- imbalance(group= nige.data$opposition_party, data=nige.data, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2",  "DumNi2", "DumSo1", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.nige.bal


nige.model <- zelig(identity_ethnic_1 ~ opposition_party + lang_d_10 + reg2 + reg3 + reg4+reg5+ reg6+ reg7+reg8 + urban + age + male + educ_basic +DumNi1 +DumNi15 + radio_daily, data = nige.data, model = "logit")
summary(nige.model)

set.seed(02138)
notopp.nige<-setx(nige.model, opposition_party=0)
opp.nige <- setx(nige.model, opposition_party=1)
sim.out.nige.opp <- sim(nige.model, x = notopp.nige, x1 = opp.nige)
summary(sim.out.nige.opp)
#0.06057808 (0.02335307 0.09701228)
#0.06629018 0.01696241 0.03349107 0.09911479

#plot(density(sim.out.nige.int$qi$fd), main="First Difference -- Ruling Party -- Nigeria", xlab="Change in Likelihood of Ethnic Id", col="darkgoldenrod", lwd=2.1, xlim=c(-.3,.2))
#abline(v=0, col="azure4", lty="dashed", lwd=.8)


################### SOUTH AFRICA ###########

sout <- subset(ruldataom, DumSo1==1 | DumSo2==1)
unique(sout$region)

sout$reg2[sout$region==2] <- 1
sout$reg2[sout$region!=2] <- 0
sout$reg3[sout$region==3] <- 1
sout$reg3[sout$region!=3] <- 0
sout$reg4[sout$region==4] <- 1
sout$reg4[sout$region!=4] <- 0
sout$reg5[sout$region==5] <- 1
sout$reg5[sout$region!=5] <- 0
sout$reg6[sout$region==6] <- 1
sout$reg6[sout$region!=6] <- 0
sout$reg7[sout$region==7] <- 1
sout$reg7[sout$region!=7] <- 0
sout$reg8[sout$region==8] <- 1
sout$reg8[sout$region!=8] <- 0
sout$reg9[sout$region==9] <- 1
sout$reg9[sout$region!=9] <- 0

pre.sout<-imbalance(group=sout$opposition_party,data=sout, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.sout

match.sout<- matchit(opposition_party ~ lang_d_10 + reg2 + reg3 + reg4 + reg5 + reg6 + reg7 +reg8+ reg9+DumSo1+ urban + age + male + educ_basic+radio_daily, data = sout, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.sout)

sout.data <- match.data(match.sout)

post.sout.bal <- imbalance(group= sout.data$opposition_party, data=sout.data, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2", "DumSo2", "DumTa1","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.sout.bal

sout.model <- zelig(identity_ethnic_1 ~ opposition_party + lang_d_10 + reg2 + reg3 + reg4+reg5+ reg6+ reg7+reg8+reg9+urban + age + male + educ_basic + DumSo1+radio_daily, data = sout.data, model = "logit")
summary(sout.model)

set.seed(02138)
notopp.sout<-setx(sout.model, opposition_party=0)
opp.sout <- setx(sout.model, opposition_party=1)
sim.out.sout.opp <- sim(sout.model, x = notopp.sout, x1 = opp.sout)
summary(sim.out.sout.opp)
#0.04678179 (-0.0008002758 0.09512108)

#plot(density(sim.out.sout.int$qi$fd), main="First Difference -- Ruling Party -- South Africa", xlab="Change in Likelihood of Ethnic Id", col="darkseagreen4", lwd=2.1, xlim=c(-.3,.2))
#abline(v=0, col="azure4", lty="dashed", lwd=.8)

################## TANZANIA #############
##### NO MATCHES IN REGION 10, we drop REGION 10 ######


tanz <- subset(ruldataom, DumTa1==1 | DumTa2==1)
unique(tanz$region)

tanz$reg2[tanz$region==2] <- 1
tanz$reg2[tanz$region!=2] <- 0
tanz$reg3[tanz$region==3] <- 1
tanz$reg3[tanz$region!=3] <- 0
tanz$reg4[tanz$region==4] <- 1
tanz$reg4[tanz$region!=4] <- 0
tanz$reg5[tanz$region==5] <- 1
tanz$reg5[tanz$region!=5] <- 0
tanz$reg6[tanz$region==6] <- 1
tanz$reg6[tanz$region!=6] <- 0
tanz$reg7[tanz$region==7] <- 1
tanz$reg7[tanz$region!=7] <- 0
tanz$reg8[tanz$region==8] <- 1
tanz$reg8[tanz$region!=8] <- 0
tanz$reg9[tanz$region==9] <- 1
tanz$reg9[tanz$region!=9] <- 0
tanz$reg10[tanz$region==10] <- 1
tanz$reg10[tanz$region!=10] <- 0
tanz$reg11[tanz$region==11] <- 1
tanz$reg11[tanz$region!=11] <- 0
tanz$reg12[tanz$region==12] <- 1
tanz$reg12[tanz$region!=12] <- 0


pre.tanz<-imbalance(group=tanz$opposition_party,data=tanz, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.tanz

match.tanz<- matchit(opposition_party ~ lang_d_10 + reg2 + reg3 + reg4 + reg5 + reg6 + reg7 +reg8+reg9+reg11+reg12+DumTa1+ urban + age + male + educ_basic+radio_daily, data = tanz, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.tanz)

tanz.data <- match.data(match.tanz)

post.tanz.bal <- imbalance(group= tanz.data$opposition_party, data=tanz.data, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2", "DumTa2", "DumUg1","DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.tanz.bal

tanz.model <- zelig(identity_ethnic_1 ~ opposition_party + lang_d_10 + reg2 + reg3 + reg4+reg5+ reg6+ reg7+reg8+reg9+reg11+reg12+urban + age + male + educ_basic + DumTa1+radio_daily, data = tanz.data, model = "logit")
summary(tanz.model)

set.seed(02138)
notopp.tanz<-setx(tanz.model, opposition_party=0)
opp.tanz <- setx(tanz.model, opposition_party=1)
sim.out.tanz.opp <- sim(tanz.model, x = notopp.tanz, x1 = opp.tanz)
summary(sim.out.tanz.opp)
#0.01092410 (-0.01710434 0.04381758)

#plot(density(sim.out.tanz$qi$fd.int), main="First Difference -- Ruling Party -- Tanzania", xlab="Change in Likelihood of Ethnic Id", col="darkorange4", lwd=2.1, xlim=c(-.3,.2))
#abline(v=0, col="azure4", lty="dashed", lwd=.8)

############ UGANDA ############################

ugan <- subset(ruldataom, DumUg1==1 | DumUg2==1)
unique(ugan$region)

ugan$reg2[ugan$region==2] <- 1
ugan$reg2[ugan$region!=2] <- 0
ugan$reg3[ugan$region==3] <- 1
ugan$reg3[ugan$region!=3] <- 0
ugan$reg4[ugan$region==4] <- 1
ugan$reg4[ugan$region!=4] <- 0

pre.ugan<-imbalance(group=ugan$opposition_party,data=ugan, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa1", "DumTa2", "DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.ugan

match.ugan<- matchit(opposition_party ~ lang_d_10 + reg2 + reg3 + reg4 + DumUg1+ urban + age + male + educ_basic+ radio_daily, data = ugan, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa1","DumTa2", "DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.ugan)

ugan.data <- match.data(match.ugan)

post.ugan.bal <- imbalance(group= ugan.data$opposition_party, data=ugan.data, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2","DumTa1", "DumTa2", "DumUg2", "DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.ugan.bal

ugan.model <- zelig(identity_ethnic_1 ~ opposition_party + lang_d_10 + reg2 +reg3 + reg4+urban + age + male + educ_basic + DumUg1+radio_daily, data = ugan.data, model = "logit")
summary(ugan.model)

set.seed(02138)
notopp.ugan<-setx(ugan.model, opposition_party=0)
opp.ugan <- setx(ugan.model, opposition_party=1)
sim.out.ugan.opp <- sim(ugan.model, x = notopp.ugan, x1 = opp.ugan)
summary(sim.out.ugan.opp)
#0.02388459 (-0.01193949 0.06483958)


#plot(density(sim.out.ugan.int$qi$fd), main="First Difference -- Ruling Party -- Uganda", xlab="Change in Likelihood of Ethnic Id", col="dodgerblue2", lwd=2.1, xlim=c(-.3,.2))
#abline(v=0, col="azure4", lty="dashed", lwd=.8)


############## Zambia ##########################

zamb <- subset(ruldataom, DumZa1==1 | DumZa2==1)
unique(zamb$region)

zamb$reg2[zamb$region==2] <- 1
zamb$reg2[zamb$region!=2] <- 0
zamb$reg3[zamb$region==3] <- 1
zamb$reg3[zamb$region!=3] <- 0
zamb$reg4[zamb$region==4] <- 1
zamb$reg4[zamb$region!=4] <- 0
zamb$reg5[zamb$region==5] <- 1
zamb$reg5[zamb$region!=5] <- 0
zamb$reg6[zamb$region==6] <- 1
zamb$reg6[zamb$region!=6] <- 0
zamb$reg7[zamb$region==7] <- 1
zamb$reg7[zamb$region!=7] <- 0

pre.zamb<-imbalance(group=zamb$opposition_party,data=zamb, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa1", "DumTa2", "DumUg1","DumUg2", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.zamb


match.zamb<- matchit(opposition_party ~ lang_d_10 + reg2 + reg3 + reg4 + reg5+reg6+reg7+DumZa1+ urban + age + male + educ_basic+radio_daily, data = zamb, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa1","DumTa2", "DumUg1", "DumUg2", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.zamb)

zamb.data <- match.data(match.zamb)

post.zamb.bal <- imbalance(group= zamb.data$opposition_party, data=zamb.data, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2","DumTa1", "DumTa2","DumUg1", "DumUg2", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.zamb.bal

zamb.model <- zelig(identity_ethnic_1 ~ opposition_party + lang_d_10 + reg2 +reg3 + reg4+ reg5+reg6+reg7+urban + age + male + educ_basic + DumZa1+radio_daily, data = zamb.data, model = "logit")
summary(zamb.model)

set.seed(02138)
notopp.zamb<-setx(zamb.model, opposition_party=0)
opp.zamb <- setx(zamb.model, opposition_party=1)
sim.out.zamb.opp <- sim(zamb.model, x = notopp.zamb, x1 = opp.zamb)
summary(sim.out.zamb.opp)
#0.02673961 (-0.01989883 0.08652014)


#plot(density(sim.out.zamb.int$qi$fd), main="First Difference -- Ruling Party -- Zambia", xlab="Change in Likelihood of Ethnic Id", col="darkslategray4", lwd=2.1, xlim=c(-.3,.2))
#abline(v=0, col="azure4", lty="dashed", lwd=.8)



################# ZIMBABWE ###########################
zimb <- subset(ruldataom, DumZi1==1 | DumZi2==1)
unique(zimb$region)

zimb$reg2[zimb$region==2] <- 1
zimb$reg2[zimb$region!=2] <- 0
zimb$reg3[zimb$region==3] <- 1
zimb$reg3[zimb$region!=3] <- 0
zimb$reg4[zimb$region==4] <- 1
zimb$reg4[zimb$region!=4] <- 0
zimb$reg5[zimb$region==5] <- 1
zimb$reg5[zimb$region!=5] <- 0
zimb$reg6[zimb$region==6] <- 1
zimb$reg6[zimb$region!=6] <- 0
zimb$reg7[zimb$region==7] <- 1
zimb$reg7[zimb$region!=7] <- 0


pre.zimb<-imbalance(group=zimb$opposition_party,data=zimb, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa1", "DumTa2", "DumUg1","DumUg2","DumZa1", "DumZa2", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.zimb

match.zimb<- matchit(opposition_party ~ lang_d_10 + reg2 + reg3 + reg4 + reg5+reg6+reg7+DumZi1+ urban + age + male + educ_basic+radio_daily, data = zimb, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa1","DumTa2", "DumUg1", "DumUg2", "DumZa1","DumZa2",  "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.zimb)

zimb.data <- match.data(match.zimb)

post.zimb.bal <- imbalance(group= zimb.data$opposition_party, data=zimb.data, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2","DumTa1", "DumTa2","DumUg1", "DumUg2","DumZa1",  "DumZa2", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.zimb.bal

zimb.model <- zelig(identity_ethnic_1 ~ opposition_party + lang_d_10 + reg2 +reg3 + reg4+ reg5+reg6+reg7+urban + age + male + educ_basic + DumZi1+radio_daily, data = zimb.data, model = "logit")
summary(zimb.model)

set.seed(02138)
notopp.zimb<-setx(zimb.model, opposition_party=0)
opp.zimb <- setx(zimb.model, opposition_party=1)
sim.out.zimb.opp <- sim(zimb.model, x = notopp.zimb, x1 = opp.zimb)
summary(sim.out.zimb.opp)
#0.04855268 (-0.001332189 0.09761319)



#plot(density(sim.out.zimb.int$qi$fd), main="First Difference -- Ruling Party -- Zimbabwe", xlab="Change in Likelihood of Ethnic Id", col="deeppink3", lwd=2.1, xlim=c(-.3,.2))
#abline(v=0, col="azure4", lty="dashed", lwd=.8)



####### Ghana ######
### LANG D 10 not coded, removed

ghan <- subset(ruldataom, DumGa2==1)
unique(ghan$region)

ghan$reg2[ghan$region==2] <- 1
ghan$reg2[ghan$region!=2] <- 0
ghan$reg3[ghan$region==3] <- 1
ghan$reg3[ghan$region!=3] <- 0
ghan$reg4[ghan$region==4] <- 1
ghan$reg4[ghan$region!=4] <- 0
ghan$reg5[ghan$region==5] <- 1
ghan$reg5[ghan$region!=5] <- 0
ghan$reg6[ghan$region==6] <- 1
ghan$reg6[ghan$region!=6] <- 0
ghan$reg7[ghan$region==7] <- 1
ghan$reg7[ghan$region!=7] <- 0
ghan$reg8[ghan$region==8] <- 1
ghan$reg8[ghan$region!=8] <- 0

table(ghan$opposition_party)

pre.ghan<-imbalance(group=ghan$opposition_party, data=ghan, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa1", "DumTa2", "DumUg1","DumUg2","DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.ghan

match.ghan<- matchit(opposition_party ~ reg2 + reg3 + reg4 + reg5+reg6+reg7+reg8+ urban + age + male + educ_basic+radio_daily, data = ghan, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa1","DumTa2", "DumUg1", "DumUg2", "DumZa1","DumZa2",  "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.ghan)

ghan.data <- match.data(match.ghan)

post.ghan.bal <- imbalance(group= ghan.data$opposition_party, data=ghan.data, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2","DumTa1", "DumTa2","DumUg1", "DumUg2","DumZa1", "DumZi1", "DumZa2", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.ghan.bal

ghan.model <- zelig(identity_ethnic_1 ~ opposition_party + reg2 +reg3 + reg4+ reg5+reg6+reg7+reg8+urban + age + male + educ_basic +radio_daily, data = ghan.data, model = "logit")
summary(ghan.model)

set.seed(02138)
oppo.ghan<-setx(ghan.model, opposition_party=0)
opp.ghan <- setx(ghan.model, opposition_party=1)
sim.out.ghan.opp <- sim(ghan.model, x = oppo.ghan, x1 = opp.ghan)
summary(sim.out.ghan.opp)
#-0.07881412 (-0.1633393 0.00761625)


######## Kenya #########
## LANG D 10 not defined, dropped

ken <- subset(ruldataom, DumKe2==1)
unique(ken$region)

ken$reg2[ken$region==2] <- 1
ken$reg2[ken$region!=2] <- 0
ken$reg3[ken$region==3] <- 1
ken$reg3[ken$region!=3] <- 0
ken$reg4[ken$region==4] <- 1
ken$reg4[ken$region!=4] <- 0
ken$reg5[ken$region==5] <- 1
ken$reg5[ken$region!=5] <- 0
ken$reg6[ken$region==6] <- 1
ken$reg6[ken$region!=6] <- 0
ken$reg7[ken$region==7] <- 1
ken$reg7[ken$region!=7] <- 0
ken$reg8[ken$region==8] <- 1
ken$reg8[ken$region!=8] <- 0

table(ken$opposition_party)

pre.ken<-imbalance(group=ken$opposition_party, data=ken, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa1", "DumTa2", "DumUg1","DumUg2","DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.ken

match.ken<- matchit(opposition_party ~ reg2 + reg3 + reg4 + reg5+reg6+reg7+reg8+ urban + age + male + educ_basic+radio_daily, data = ken, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa1","DumTa2", "DumUg1", "DumUg2", "DumZa1","DumZa2",  "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.ken)

ken.data <- match.data(match.ken)

post.ken.bal <- imbalance(group= ken.data$opposition_party, data=ken.data, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2","DumTa1", "DumTa2","DumUg1", "DumUg2","DumZa1", "DumZi1", "DumZa2", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.ken.bal

ken.model <- zelig(identity_ethnic_1 ~ opposition_party + reg2 +reg3 + reg4+ reg5+reg6+reg7+reg8+urban + age + male + educ_basic +radio_daily, data = ken.data, model = "logit")
summary(ken.model)

set.seed(02138)
oppo.ken<-setx(ken.model, opposition_party=0)
opp.ken <- setx(ken.model, opposition_party=1)
sim.out.ken.opp <- sim(ken.model, x = oppo.ken, x1 = opp.ken)
summary(sim.out.ken.opp)
#0.04580406 (0.0067875 0.0929912)



####### Mozambique ########
##### Very low numbers of opposition supporters create huge standard errors

#no matches in regions 2, 8, dropped
#no data on lang_d_10


#moz <- subset(ruldataom, DumMo2==1)
#unique(moz$region)
#
#moz$reg2[moz$region==2] <- 1
#moz$reg2[moz$region!=2] <- 0
#moz$reg3[moz$region==3] <- 1
#moz$reg3[moz$region!=3] <- 0
#moz$reg4[moz$region==4] <- 1
#moz$reg4[moz$region!=4] <- 0
#moz$reg5[moz$region==5] <- 1
#moz$reg5[moz$region!=5] <- 0
#moz$reg6[moz$region==6] <- 1
#moz$reg6[moz$region!=6] <- 0
#moz$reg7[moz$region==7] <- 1
#moz$reg7[moz$region!=7] <- 0
#moz$reg8[moz$region==8] <- 1
#moz$reg8[moz$region!=8] <- 0
#moz$reg9[moz$region==9] <- 1
#moz$reg9[moz$region!=9] <- 0
#moz$reg10[moz$region==10] <- 1
#moz$reg10[moz$region!=10] <- 0
#moz$reg11[moz$region==11] <- 1
#moz$reg11[moz$region!=11] <- 0
#
#table(moz$opposition_party)
#
#pre.moz<-imbalance(group=moz$opposition_party, data=moz, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa1", "DumTa2", "DumUg1","DumUg2","DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
#pre.moz
#
#match.moz<- matchit(opposition_party ~ reg8 + reg4 + reg3  + reg5+reg6+reg7+ reg9 + reg10 + reg11 + urban + age + male + educ_basic+radio_daily, data = moz, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa1","DumTa2", "DumUg1", "DumUg2", "DumZa1","DumZa2",  "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
#summary(match.moz)
#
#moz.data <- match.data(match.moz)
#
#post.moz.bal <- imbalance(group= moz.data$opposition_party, data=moz.data, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2","DumTa1", "DumTa2","DumUg1", "DumUg2","DumZa1", "DumZi1", "DumZa2", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
#post.moz.bal
#
#moz.model <- zelig(identity_ethnic_1 ~ opposition_party + reg4 + reg3 + reg5+reg6+reg7+reg8+reg9 + reg10 + reg11 + urban + age + male + educ_basic +radio_daily, data = moz.data, model = "logit")
#summary(moz.model)
#
#set.seed(02138)
#oppo.moz<-setx(moz.model, opposition_party=0)
#opp.moz <- setx(moz.model, opposition_party=1)
#sim.out.moz.opp <- sim(moz.model, x = oppo.moz, x1 = opp.moz)
#summary(sim.out.moz.opp)
##-0.07195057 (-0.1419360 -0.00461381)
#


###### Senegal ##########

sene <- subset(ruldataom, DumSen2==1)
unique(sene$region)

sene$reg2[sene$region==2] <- 1
sene$reg2[sene$region!=2] <- 0
sene$reg3[sene$region==3] <- 1
sene$reg3[sene$region!=3] <- 0
sene$reg4[sene$region==4] <- 1
sene$reg4[sene$region!=4] <- 0
sene$reg5[sene$region==5] <- 1
sene$reg5[sene$region!=5] <- 0
sene$reg6[sene$region==6] <- 1
sene$reg6[sene$region!=6] <- 0


table(sene$opposition_party)

pre.sene<-imbalance(group=sene$opposition_party, data=sene, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp", "DumBo1", "DumBo2", "DumMa1", "DumMa2",  "DumMali1","DumMali2","DumNa1", "DumNa15", "DumNa2","DumNi1", "DumNi15", "DumNi2", "DumSo1","DumSo2", "DumTa1", "DumTa2", "DumUg1","DumUg2","DumZa1", "DumZa2", "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"))
pre.sene

match.sene<- matchit(opposition_party ~ reg2 + reg3 + reg4 + reg5+reg6+ urban + age + male + educ_basic+radio_daily, data = sene, drop = c("identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1" ,"DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi14","DumNi2","DumSo1", "DumSo2","DumTa1","DumTa2", "DumUg1", "DumUg2", "DumZa1","DumZa2",  "DumZi1", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2"),  cutpoints = mycp, k2k = FALSE, method="cem")
summary(match.sene)

sene.data <- match.data(match.sene)

post.sene.bal <- imbalance(group= sene.data$opposition_party, data=sene.data, drop = c("opposition_party", "identity_ethnic_1", "region", "elec_prox", "prox_comp", "elec_comp","DumBo1", "DumBo2", "DumMa1", "DumMa2","DumMali1", "DumMali2","DumNa1", "DumNa15", "DumNa2", "DumNi1", "DumNi15", "DumNi2","DumSo1", "DumSo2","DumTa1", "DumTa2","DumUg1", "DumUg2","DumZa1", "DumZi1", "DumZa2", "DumZi2", "DumGa2", "DumKe2", "DumMo2", "DumSen2", "distance", "weights", "subclass"))
post.sene.bal

sene.model <- zelig(identity_ethnic_1 ~ opposition_party + reg2 +reg3 + reg4 + reg5 + reg6 +urban + age + male + educ_basic +radio_daily, data = sene.data, model = "logit")
summary(sene.model)

set.seed(02138)
oppo.sene<-setx(sene.model, opposition_party=0)
opp.sene <- setx(sene.model, opposition_party=1)
sim.out.sene.opp <- sim(sene.model, x = oppo.sene, x1 = opp.sene)
summary(sim.out.sene.opp)
#-0.02641877 (-0.05150897 0.1036508)



############# CROSS NATIONAL ESTIMATES FOR OPPOSITION PARTY SUPPORTERS ###############

cross.national <- rbind(zimb.data[,c(1:38, 45:47)], zamb.data[, c(1:38, 45:47)], ugan.data[, c(1:38, 42:44)], tanz.data[, c(1:38, 50:52)], sout.data[, c(1:38, 47:49)], nige.data[ , c(1:38, 46:48)], nami.data[, c(1:38, 48:50)], mala.data[ , c(1:38, 41:43)], bots.data[ , c(1:38, 42:44)], ghan.data[ , c(1:38, 46:48)], ken.data[ , c(1:38, 46:48)], sene.data[ , c(1:38, 44:46)])
dim(cross.national)
#[1] [1] 22393    41

cross.national$nation.wts[cross.national$DumBo1==1|cross.national$DumBo2==1] <- 1 / nrow(bots.data)
cross.national$nation.wts[cross.national$DumMa1==1|cross.national$DumMa2==1] <- 1 / nrow(mala.data)
#cross.national$nation.wts[cross.national$DumMali1==1|cross.national$DumMali2==1] <- 1 / nrow(mali.data)
cross.national$nation.wts[cross.national$DumNa1==1|cross.national$DumNa2==1|cross.national$DumNa15==1] <- 1 / nrow(nami.data)
cross.national$nation.wts[cross.national$DumNi1==1|cross.national$DumNi2==1|cross.national$DumNi15==1] <- 1 / nrow(nige.data)
cross.national$nation.wts[cross.national$DumSo1==1|cross.national$DumSo2==1] <- 1 / nrow(sout.data)
cross.national$nation.wts[cross.national$DumTa1==1|cross.national$DumTa2==1] <- 1 / nrow(tanz.data)
cross.national$nation.wts[cross.national$DumUg1==1|cross.national$DumUg2==1] <- 1 / nrow(ugan.data)
cross.national$nation.wts[cross.national$DumZa1==1|cross.national$DumZa2==1] <- 1 / nrow(zamb.data)
cross.national$nation.wts[cross.national$DumZi1==1|cross.national$DumZi2==1] <- 1 / nrow(zimb.data)
cross.national$nation.wts[cross.national$DumGa2==1] <- 1 / nrow(ghan.data)
cross.national$nation.wts[cross.national$DumKe2==1] <- 1 / nrow(ken.data)
#cross.national$nation.wts[cross.national$DumMo2==1] <- 1 / nrow(moz.data)
cross.national$nation.wts[cross.national$DumSen2==1] <- 1 / nrow(sene.data)

#real weights is country weights * nation weights
cross.national$real.weights <- cross.national$weights * cross.national$nation.wts * 1000

#make round/country dummies separately
cross.national$bots[cross.national$DumBo1==1|cross.national$DumBo2==1] <- 1
cross.national$bots[cross.national$DumBo1!=1&cross.national$DumBo2!=1] <- 0
cross.national$mala[cross.national$DumMa1==1|cross.national$DumMa2==1] <- 1
cross.national$mala[cross.national$DumMa1!=1&cross.national$DumMa2!=1] <- 0
#cross.national$mali[cross.national$DumMali1==1|cross.national$DumMali2==1] <- 1
#cross.national$mali[cross.national$DumMali1!=1&cross.national$DumMali2!=1] <- 0
cross.national$nami[cross.national$DumNa1==1|cross.national$DumNa2==1|cross.national$DumNa15==1] <- 1
cross.national$nami[cross.national$DumNa1!=1&cross.national$DumNa2!=1&cross.national$DumNa15!=1] <- 0
cross.national$nige[cross.national$DumNi1==1|cross.national$DumNi2==1|cross.national$DumNi15==1] <- 1
cross.national$nige[cross.national$DumNi1!=1&cross.national$DumNi2!=1&cross.national$DumNi15!=1] <- 0
cross.national$sout[cross.national$DumSo1==1|cross.national$DumSo2==1] <- 1 
cross.national$sout[cross.national$DumSo1!=1&cross.national$DumSo2!=1] <- 0
cross.national$tanz[cross.national$DumTa1==1|cross.national$DumTa2==1] <- 1
cross.national$tanz[cross.national$DumTa1!=1&cross.national$DumTa2!=1] <- 0
cross.national$ugan[cross.national$DumUg1==1|cross.national$DumUg2==1] <- 1
cross.national$ugan[cross.national$DumUg1!=1&cross.national$DumUg2!=1] <- 0
cross.national$zamb[cross.national$DumZa1==1|cross.national$DumZa2==1] <- 1
cross.national$zamb[cross.national$DumZa1!=1&cross.national$DumZa2!=1] <- 0
cross.national$zimb[cross.national$DumZi1==1|cross.national$DumZi2==1] <- 1
cross.national$zimb[cross.national$DumZi1!=1&cross.national$DumZi2!=1] <- 0

cross.national$r15[cross.national$DumNa15==1|cross.national$DumNi15==1] <- 1
cross.national$r15[cross.national$DumNa15!=1&cross.national$DumNi15!=1] <- 0
cross.national$r2[cross.national$DumBo2==1|cross.national$DumMa2==1|cross.national$DumNa2==1|cross.national$DumNi2==1|cross.national$DumSo2==1|cross.national$DumTa2==1|cross.national$DumUg2==1|cross.national$DumZa2==1|cross.national$DumZi2==1] <- 1
cross.national$r2[cross.national$DumBo2!=1&cross.national$DumMa2!=1&cross.national$DumNa2!=1&cross.national$DumNi2!=1&cross.national$DumSo2!=1&cross.national$DumTa2!=1&cross.national$DumUg2!=1&cross.national$DumZa2!=1&cross.national$DumZi2!=1] <- 0

dim(cross.national)
names(cross.national)


########TABLE 4, MODEL 1 (with all countries)############
### just matching vars
#lang is matched on in some subsets, but dropped because of the 0's in other countries

model1 <- zelig(identity_ethnic_1 ~ opposition_party + urban + age + male + educ_basic + radio_daily + r2 +r15, data = cross.national, model = "logit", weights = cross.national$real.weights)
summary(model1)


not.opp <- setx(model1, opposition_party=0)
yes.opp <- setx(model1, opposition_party=1)
sim.out.cross.nat <- sim(model1, x=not.opp, x1=yes.opp)
summary(sim.out.cross.nat)

####### TABLE 4, MODEL 2 #########
### with elec_prox elec_comp prox_comp

model2 <- zelig(identity_ethnic_1 ~ opposition_party + elec_prox + elec_comp + prox_comp + urban + age + male + educ_basic + radio_daily + r2, data = cross.national, model = "logit", weights = cross.national$real.weights)
summary(model2)


not.opp <- setx(model2, opposition_party=0)
yes.opp <- setx(model2, opposition_party=1)
sim.out.cross.nat.2 <- sim(model2, x=not.opp, x1=yes.opp)
summary(sim.out.cross.nat.2)


########### Without Ghana, Kenya, Mozambique and Senegal additional countries ###########


cross.national.abbrev <- subset(cross.national, DumGa2!=1&DumKe2!=1&DumMo2!=1&DumSen2!=1)

# MODEL 1, with fixed effects
#model1 <- zelig(identity_ethnic_1 ~ opposition_party + urban + age + male + educ_basic + radio_daily + mala + nami + nige + sout + tanz + ugan + zamb + zimb + r2, data = cross.national.abbrev, model = "logit", weights = cross.national.abbrev$real.weights)
#summary(model1)
#
#model1a <- zelig(identity_ethnic_1 ~ opposition_party + I(opposition_party*mala) + I(opposition_party*nami) + I(opposition_party*nige) + I(opposition_party*sout) + I(opposition_party*tanz) + I(opposition_party*ugan) + I(opposition_party*zamb) + I(opposition_party*zimb) + urban + age + male + educ_basic + radio_daily + mala + nami + nige + sout + tanz + ugan + zamb + zimb + r2, data = cross.national.abbrev, model = "logit", weights = cross.national.abbrev$real.weights)
#summary(model1a)


###### Table 4, Model 3 ########
### with elec_prox elec_comp prox_comp

model2 <- zelig(identity_ethnic_1 ~ opposition_party + elec_prox + elec_comp + prox_comp + urban + age + male + educ_basic + radio_daily + mala + nami + nige + sout + tanz + ugan + zamb + zimb + r15 + r2, data = cross.national.abbrev, model = "logit", weights = cross.national.abbrev$real.weights)
summary(model2)

not.opp <- setx(model2, opposition_party=0)
yes.opp <- setx(model2, opposition_party=1)
sim.out.cross.nat.2 <- sim(model2, x=not.opp, x1=yes.opp)
summary(sim.out.cross.nat.2)


############################################### SECTION FIVE #####################################
####################################### PLOTS 
##################################################################################################

###### Balance Plot - Figure 1 in Paper #####

pdf(file = "balance_plot_for_paper.pdf", height=7, width=6)
par(oma=c(0,4.4,0,0))
plot(x = (summary(match.nige)$sum.all[,1] - summary(match.nige)$sum.all[,2])/summary(match.nige)$sum.all[,3], y = 1:nrow(summary(match.nige)$sum.all), pch = 19, col = "orange", xlab = "Standardized Imbalance", ylab = "", main="Pre- and Post-Matching, Nigeria", xlim=c(-.8,.8), yaxt="n")
points(x = (summary(match.nige)$sum.matched[,1] - summary(match.nige)$sum.matched[,2])/summary(match.nige)$sum.matched[,3], y = 1:nrow(summary(match.nige)$sum.matched), pch =17 , col = "skyblue")
abline(v = 0, lty = "dashed")
axis(1) 
axis(2, at = 1:nrow(summary(match.nige)$sum.matched), labels = c("Overall Distance", "Lang. Group Size", "Central States", "Northeastern States", "Northwestern States", "Southeastern States", "Niger Delta States", "Southwestern States", "Abuja", "Round 1 Survey", "Round 1.5 Survey", "Urban", "Age", "Gender", "Primary Education", "Daily Radio Listener"), las = 1, cex.axis = .8)
#axis(2, at = 1:nrow(summary(match.nige)$sum.matched), labels = rownames(summary(match.nige)$sum.matched),las = 1, cex.axis = .8)
legend("topright", legend = c("Pre-match","Post-match"), pch = c(19,17), col = c("orange","skyblue"), cex = .8)
dev.off()

######## Other balance plots- not included in the paper ########
##pdf(file="compiled_rul_balance.pdf")
#par(mfrow=c(2,5))
#plot(x = (summary(match.bots)$sum.all[,1] - summary(match.bots)$sum.all[,2])/summary(match.bots)$sum.all[,3], y = 1:nrow(summary(match.bots)$sum.all), pch = 19, col = "orange", xlab = "Standardized Imbalance", axes = FALSE, xlim = c(-2,2), ylab = "", main="Imbalance Before and After Matching, Botswana")
#points(x = (summary(match.bots)$sum.matched[,1] - summary(match.bots)$sum.matched[,2])/summary(match.bots)$sum.matched[,3], y = 1:nrow(summary(match.bots)$sum.matched), pch = 19, col = "skyblue")
#abline(v = 0, lty = "dashed")
#axis(1) 
#axis(2, at = 1:nrow(summary(match.bots)$sum.matched), labels = rownames(summary(match.bots)$sum.matched),
#las = 1, cex.axis = .8)
#legend("topright", legend = c("Pre-match","Post-match"), pch = c(19,19), col = c("orange","skyblue"), cex = .8)
#
#plot(x = (summary(match.mala)$sum.all[,1] - summary(match.mala)$sum.all[,2])/summary(match.mala)$sum.all[,3], y = 1:nrow(summary(match.mala)$sum.all), pch = 19, col = "orange", xlab = "Standardized Imbalance", axes = FALSE, xlim = c(-2,2), ylab = "", main="Imbalance Before and After Matching, Malawi")
#points(x = (summary(match.mala)$sum.matched[,1] - summary(match.mala)$sum.matched[,2])/summary(match.mala)$sum.matched[,3], y = 1:nrow(summary(match.mala)$sum.matched), pch = 19, col = "skyblue")
#abline(v = 0, lty = "dashed")
#axis(1) 
#axis(2, at = 1:nrow(summary(match.mala)$sum.matched), labels = rownames(summary(match.mala)$sum.matched),las = 1, cex.axis = .8)
#legend("topright", legend = c("Pre-match","Post-match"), pch = c(19,19), col = c("orange","skyblue"), cex = .8)
#
#plot(x = (summary(match.nami)$sum.all[,1] - summary(match.nami)$sum.all[,2])/summary(match.nami)$sum.all[,3], y = 1:nrow(summary(match.nami)$sum.all), pch = 19, col = "orange", xlab = "Standardized Imbalance", axes = FALSE, ylab = "", main="Namibia", xlim=c(-2,2))
#points(x = (summary(match.nami)$sum.matched[,1] - summary(match.nami)$sum.matched[,2])/summary(match.nami)$sum.matched[,3], y = 1:nrow(summary(match.nami)$sum.matched), pch = 19, col = "skyblue")
#abline(v = 0, lty = "dashed")
#axis(1) 
#axis(2, at = 1:nrow(summary(match.nami)$sum.matched), labels = rownames(summary(match.nami)$sum.matched),las = 1, cex.axis = .8)
#legend("topright", legend = c("Pre-match","Post-match"), pch = c(19,19), col = c("orange","skyblue"), cex = .8)
#
#plot(x = (summary(match.nige)$sum.all[,1] - summary(match.nige)$sum.all[,2])/summary(match.nige)$sum.all[,3], y = 1:nrow(summary(match.nige)$sum.all), pch = 19, col = "orange", xlab = "Standardized Imbalance", axes = FALSE, ylab = "", main="Nigeria", xlim=c(-2,2))
#points(x = (summary(match.nige)$sum.matched[,1] - summary(match.nige)$sum.matched[,2])/summary(match.nige)$sum.matched[,3], y = 1:nrow(summary(match.nige)$sum.matched), pch = 19, col = "skyblue")
#abline(v = 0, lty = "dashed")
#axis(1) 
#axis(2, at = 1:nrow(summary(match.nige)$sum.matched), labels = rownames(summary(match.nige)$sum.matched),las = 1, cex.axis = .8)
#legend("topright", legend = c("Pre-match","Post-match"), pch = c(19,19), col = c("orange","skyblue"), cex = .8)
#
#plot(x = (summary(match.sout)$sum.all[,1] - summary(match.sout)$sum.all[,2])/summary(match.sout)$sum.all[,3], y = 1:nrow(summary(match.sout)$sum.all), pch = 19, col = "orange", xlab = "Standardized Imbalance", axes = FALSE, ylab = "", main="South Africa", xlim=c(-2,2))
#points(x = (summary(match.sout)$sum.matched[,1] - summary(match.sout)$sum.matched[,2])/summary(match.sout)$sum.matched[,3], y = 1:nrow(summary(match.sout)$sum.matched), pch = 19, col = "skyblue")
#abline(v = 0, lty = "dashed")
#axis(1) 
#axis(2, at = 1:nrow(summary(match.sout)$sum.matched), labels = rownames(summary(match.sout)$sum.matched),las = 1, cex.axis = .8)
#legend("topright", legend = c("Pre-match","Post-match"), pch = c(19,19), col = c("orange","skyblue"), cex = .8)
#
#plot(x = (summary(match.tanz)$sum.all[,1] - summary(match.tanz)$sum.all[,2])/summary(match.tanz)$sum.all[,3], y = 1:nrow(summary(match.tanz)$sum.all), pch = 19, col = "orange", xlab = "Standardized Imbalance", axes = FALSE, ylab = "", main="Tanzania", xlim=c(-2,2))
#points(x = (summary(match.tanz)$sum.matched[,1] - summary(match.tanz)$sum.matched[,2])/summary(match.tanz)$sum.matched[,3], y = 1:nrow(summary(match.tanz)$sum.matched), pch = 19, col = "skyblue")
#abline(v = 0, lty = "dashed")
#axis(1) 
#axis(2, at = 1:nrow(summary(match.tanz)$sum.matched), labels = rownames(summary(match.tanz)$sum.matched),las = 1, cex.axis = .8)
#legend("topright", legend = c("Pre-match","Post-match"), pch = c(19,19), col = c("orange","skyblue"), cex = .8)
#
#plot(x = (summary(match.ugan)$sum.all[,1] - summary(match.ugan)$sum.all[,2])/summary(match.ugan)$sum.all[,3], y = 1:nrow(summary(match.ugan)$sum.all), pch = 19, col = "orange", xlab = "Standardized Imbalance", axes = FALSE, ylab = "", main="Uganda", xlim=c(-2,2))
#points(x = (summary(match.ugan)$sum.matched[,1] - summary(match.ugan)$sum.matched[,2])/summary(match.ugan)$sum.matched[,3], y = 1:nrow(summary(match.ugan)$sum.matched), pch = 19, col = "skyblue")
#abline(v = 0, lty = "dashed")
#axis(1) 
#axis(2, at = 1:nrow(summary(match.ugan)$sum.matched), labels = rownames(summary(match.ugan)$sum.matched),las = 1, cex.axis = .8)
#legend("topright", legend = c("Pre-match","Post-match"), pch = c(19,19), col = c("orange","skyblue"), cex = .8)
#
#plot(x = (summary(match.zamb)$sum.all[,1] - summary(match.zamb)$sum.all[,2])/summary(match.zamb)$sum.all[,3], y = 1:nrow(summary(match.zamb)$sum.all), pch = 19, col = "orange", xlab = "Standardized Imbalance", axes = FALSE, ylab = "", main="Zambia", xlim=c(-2,2))
#points(x = (summary(match.zamb)$sum.matched[,1] - summary(match.zamb)$sum.matched[,2])/summary(match.zamb)$sum.matched[,3], y = 1:nrow(summary(match.zamb)$sum.matched), pch = 19, col = "skyblue")
#abline(v = 0, lty = "dashed")
#axis(1) 
#axis(2, at = 1:nrow(summary(match.zamb)$sum.matched), labels = rownames(summary(match.zamb)$sum.matched),las = 1, cex.axis = .8)
#legend("topright", legend = c("Pre-match","Post-match"), pch = c(19,19), col = c("orange","skyblue"), cex = .8)
#
#plot(x = (summary(match.zimb)$sum.all[,1] - summary(match.zimb)$sum.all[,2])/summary(match.zimb)$sum.all[,3], y = 1:nrow(summary(match.zimb)$sum.all), pch = 19, col = "orange", xlab = "Standardized Imbalance", axes = FALSE, ylab = "", main="Zimbabwe", xlim=c(-2,2))
#points(x = (summary(match.zimb)$sum.matched[,1] - summary(match.zimb)$sum.matched[,2])/summary(match.zimb)$sum.matched[,3], y = 1:nrow(summary(match.zimb)$sum.matched), pch = 19, col = "skyblue")
#abline(v = 0, lty = "dashed")
#axis(1) 
#axis(2, at = 1:nrow(summary(match.zimb)$sum.matched), labels = rownames(summary(match.zimb)$sum.matched),las = 1, cex.axis = .8)
#legend("topright", legend = c("Pre-match","Post-match"), pch = c(19,19), col = c("orange","skyblue"), cex = .8)
#dev.off()

##############################################
#### First difference- Figure 2 in Paper #####
#FIRST: RUN RUPAR and OPPO matching then:

pdf(file="ATT_panels_0425.pdf", height=8, width=6)
par(mfrow=c(2,2))

plot(density(sim.out.ken.ru$qi$fd), main="Kenya", xlab="Change in Ethnic Id", col="dodgerblue2", xlim=c(-.25, .25), lwd=1.6, yaxt="n")
axis(2, at=c(5,10,15,20,25), labels=c("", "", "", "", ""))
abline(v=0, col="azure4", lty="dashed", lwd=.8)
lines(density(sim.out.ken.opp$qi$fd), col="firebrick1", lty="dotted", lwd=2.5)

plot(density(sim.out.mala.ru$qi$fd), main="Malawi", xlab="Change in Ethnic Id", col="dodgerblue2", xlim=c(-.25, .25), lwd=1.6, yaxt="n")
axis(2, at=c(5,10,15,20,25), labels=c("", "", "", "", ""))
abline(v=0, col="azure4", lty="dashed", lwd=.8)
lines(density(sim.out.mala.opp$qi$fd), col="firebrick1", lty="dotted", lwd=2.5)

plot(density(sim.out.nige.ru$qi$fd), main="Nigeria", xlab="Change in Ethnic Id", col="dodgerblue2", xlim=c(-.25, .25), lwd=1.6, yaxt="n")
axis(2, at=c(5,10,15,20,25), labels=c("", "", "", "", ""))
abline(v=0, col="azure4", lty="dashed", lwd=.8)
lines(density(sim.out.nige.opp$qi$fd), col="firebrick1", lty="dotted", lwd=2.5)


plot(density(sim.out.sout.ru$qi$fd), main="South Africa", xlab="Change in Ethnic Id", col="dodgerblue2", xlim=c(-.25, .25), lwd=1.6, yaxt="n")
axis(2, at=c(5,10,15,20,25), labels=c("", "", "", "", ""))
abline(v=0, col="azure4", lty="dashed", lwd=.8)
lines(density(sim.out.sout.opp$qi$fd), col="firebrick1", lty="dotted", lwd=2.5)


dev.off()



